package com.xunyoutest.shop.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xunyoutest.shop.R;
import com.xunyoutest.shop.utils.ClickEventUtils;

public class InquiryDialog extends Dialog {

    private Button yes;//确定按钮
    private Button no;//取消按钮
    private TextView messageTv;//消息提示文本
    private TextView titleTv;
    private String messageStr,titleStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;
    private View line;
    private RelativeLayout rel_layout;
    private View customView;//用户自定义View

    private boolean isDialogHeight=true;//是否正常显示dialog，true：是 false: 不，要自定义高度

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    private int yesDrawable=-1;

    public InquiryDialog(Context context) {
        super(context, R.style.InquiryDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_dialog_inquiry);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        titleTv= findViewById(R.id.title);
        messageTv = findViewById(R.id.message);
        yes = findViewById(R.id.yes);
        no = findViewById(R.id.no);
        line = findViewById(R.id.line);
        rel_layout= findViewById(R.id.rel_layout);
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr!=null){
            titleTv.setText(titleStr);
        }
        if (customView!=null){
            if (!isDialogHeight){
                customView.setLayoutParams(new RelativeLayout.LayoutParams(getScreenWidth(), (int) (getScreenHeight() / 1.5)));
            }
            rel_layout.addView(customView);
            rel_layout.setVisibility(View.VISIBLE);
            messageTv.setVisibility(View.GONE);
        }else if (messageStr != null) {
            messageTv.setText(messageStr);
        }else {
            messageTv.setVisibility(View.GONE);
        }
        //如果设置按钮的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }else {
            yes.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
        if (noStr != null) {
            no.setText(noStr);
        }else {
            no.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
        if (yesDrawable!=-1){
            yes.setBackgroundResource(yesDrawable);
            yes.setTextColor(getContext().getResources().getColor(R.color.white));
        }
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickEventUtils.isFastDoubleClick(v.getId())) {
                    if (yesOnclickListener != null) {
                        yesOnclickListener.onYesClick();
                    }
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickEventUtils.isFastDoubleClick(v.getId())) {
                    if (noOnclickListener != null) {
                        noOnclickListener.onNoClick();
                    }
                }
            }
        });
    }

    /**
     * 从外界Activity为Dialog设置自定义按钮样式
     * @param drawable
     */
    public InquiryDialog setYesDrawable(int drawable){
        this.yesDrawable=drawable;
        return this;
    }

    /**
     * 从外界Activity为Dialog设置自定义View
     * @param View
     */
    public InquiryDialog setCustomView(View View) {
        customView = View;
        return this;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     * @param message
     */
    public InquiryDialog setMessage(String message) {
        messageStr = message;
        return this;
    }

    /**
     * 从外界Activity为Dialog设置dialog的Title
     * @param title
     */
    public InquiryDialog setTitle(String title) {
        titleStr = title;
        return this;
    }

    /**
     * 设置取消按钮的显示内容和监听
     * @param str
     * @param onNoOnclickListener
     */
    public InquiryDialog setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
        return this;
    }

    /**
     * 设置确定按钮的显示内容和监听
     * @param str
     * @param onYesOnclickListener
     */
    public InquiryDialog setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
        return this;
    }

    /***
     * 是否适应Dialog的原本的高度
     * true：是 false: 不，要自定义高度
     * @param dialogHeight
     */
    public InquiryDialog setDialogHeight(boolean dialogHeight) {
        isDialogHeight = dialogHeight;
        return this;
    }

    /**
     * 获取屏幕高度(px)
     */
    private int getScreenHeight() {
        return getContext().getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 获取屏幕宽度(px)
     */
    private int getScreenWidth() {
        return getContext().getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        void onYesClick();
    }

    public interface onNoOnclickListener {
        void onNoClick();
    }
}